<?php
 class AscetPDO extends PDO{
  var $last_query_duration;
  var $transactions_count   =0;
  var $transaction_level    ='';
  // @todo Вставить слежение за включением/выключением транзакций

  function sql_interface($sql, $type){
   $begin_stamp=microtime(true);
   if ($type==1){
    $q=$this->query($sql);
   }else{
    $q=$this->exec($sql);
   }
   $end_stamp  =microtime(true);
   $this->last_query_duration=$end_stamp-$begin_stamp;
   
   $error_info=$this->errorInfo();
   if ($error_info[0]!='00000'){
    throw new PDOException('SQLSTATE['.$error_info[0].'] '.$error_info[2], (int)$error_info[0]);
   }
   
   if ($type==1){
    $q->setFetchMode(PDO::FETCH_ASSOC);
    return $q;
   }
  }
  
  function sql_query_fetch($sql){
   $q=$this->sql_interface($sql, 1);
   if (gettype($q)!='object'){return NULL;}
   return $q->fetch();
  }
  
  function sql_exec($sql){
   $this->sql_interface($sql, 0);
  }
  
  function sql_query($sql){
   return $this->sql_interface($sql, 1);
  }
  
  function sql_insert_id(){
   return (int)$this->lastInsertId();
  }
  
  function sql_next_transaction(){
   $this->commit();
   $this->beginTransaction();
   $this->transactions_count++;
  }

  function sql_level($transaction_level=''){
   if ($transaction_level==''){$transaction_level='read uncommitted';}
   if ($transaction_level==$this->transaction_level){return;}
   $this->transaction_level=$transaction_level;
   $this->commit();
   $this->exec('set session transaction isolation level '.$transaction_level.';');
   $this->beginTransaction();
   $this->transactions_count++;
  }

 }// class AscetPDO




// Global functions, неиспользующие AscetCMSEngine
 function sad_safe_mysql($text){
  $a=array(
   array('\\',   '\\\\'),
   array('"',    '\\"'),
   array("'",    "\\'"),
   array(chr(0), '\\0'),
   array('`',    '\\`')
  );
  foreach($a as $pair){$text=str_replace($pair[0], $pair[1], $text);}
  return $text;
 }
 
 function sql_num($q){
  if(gettype($q)!='object'){
   return 0;
  }
  return $q->rowCount();
 }

 function sql_fetch($q){
  if (gettype($q)!='object'){return NULL;}
  return $q->fetch();
 }

 function sql_fetch_all($q, $fields=array(), $is_object=1){
  if (gettype($q)!='object'){return array();}
  $fields=(object)$fields;

  $value=array();
  while ($q_c=$q->fetch()){
   $datum = $q_c;
   if(isset($fields->int)){
    foreach($fields->int as $key){
     $datum[$key] = (int)$datum[$key];
    }
   }
   if(isset($fields->float)){
    foreach($fields->float as $key){
     $datum[$key] = (float)$datum[$key];
    }
   }

   $value[]=($is_object==1) ? (object)$datum : $datum;
  }

  return $value;
 }


?>